<?php

/*-----------------------------------------------------------------------------------------*/
/*	Metabox Attachments AJAX Update
/*-----------------------------------------------------------------------------------------*/

function si_gallery_metabox_ajax_update() {
	if ( !empty( $_POST['post_id'] ) )  {
			$args = array(
					'post_type' => 'attachment',
					'post_status' => 'inherit',
					'post_parent' => $_POST['post_id'],
					'post_mime_type' => 'image',
					'posts_per_page' => '-1',
					'order' => 'ASC',
					'orderby' => 'menu_order',
					'exclude'     => get_post_thumbnail_id($_POST['post_id'])
				);				
				
			$attachments = get_posts( $args );
			$return = '';						
			if( empty( $attachments ) )
				$return .= '<p>'. __('No images.','si_theme'). '</p>';	
			else {	
					foreach( $attachments as $image ):
						$thumbnail = wp_get_attachment_image_src( $image->ID, 'thumbnail');
						$return .= '<img style="margin-right:5px;" width="100" height="100" src="' . $thumbnail[0] . '" alt="' . apply_filters('the_title', $image->post_title). '"/>';
					endforeach;
			}			
			echo $return;
			exit();
	}
}

add_action( 'wp_ajax_refresh_metabox', 'si_gallery_metabox_ajax_update' );


/*-------------------------------------------------------------------------------------------*/
/*	Add Custom Fields to Media Uploader
/*  - Video URL field for Gallery Template
/*  - Image Size % field for Portfolio & Gallery Post Format
/*-------------------------------------------------------------------------------------------*/

function si_attachment_field_custom( $form_fields, $post ) {
	
	// Only show on the in-page media uploader
	$screen = get_current_screen();
	if( 'media-upload' !== $screen->base )
		return $form_fields;
	
	// Only show when gallery template selected
	$current_template = get_post_meta( $post->post_parent, '_wp_page_template', true );
	if( 'template-gallery.php' == $current_template ){
		$form_fields['si-attachment-video-url'] = array(
			'label' => __('Video Link URL','si_theme'),
			'input' => 'text',
			'value' => get_post_meta( $post->ID, 'si-attachment-video-url', true ),
			'helps' => __('Enter the url of a youtube or vimeo video that will open in the lightbox','si_theme'),
		);
	}
		
	// Only show when custom post type is portfolio or post format is gallery
	if( 'portfolio' == get_post_type( $post->post_parent ) || 'gallery' == get_post_format( $post->post_parent) ){
		$form_fields['si-attachment-percentage-size'] = array(
			'label' => __('Image Size %','si_theme'),
			'input' => 'text',
			'value' => get_post_meta( $post->ID, 'si-attachment-percentage-size', true ),
			'helps' => __('Enter the percentage that you want to apply to the image (from 1 to 100)','si_theme'),
		);
	}
		
	return $form_fields;
}

add_filter( 'attachment_fields_to_edit', 'si_attachment_field_custom', 10, 2 );


/*-----------------------------------------------------------------------------------------*/
/*	Save Custom Values in media uploader
/*-----------------------------------------------------------------------------------------*/

function si_attachment_field_custom_save( $post, $attachment ) {
	if( isset( $attachment['si-attachment-video-url'] ) )
		update_post_meta( $post['ID'], 'si-attachment-video-url', $attachment['si-attachment-video-url'] );
	
	if( isset( $attachment['si-attachment-percentage-size'] ) )
		update_post_meta( $post['ID'], 'si-attachment-percentage-size', $attachment['si-attachment-percentage-size'] );
	
	return $post;
}

add_filter( 'attachment_fields_to_save', 'si_attachment_field_custom_save', 10, 2 );

?>